<?php

namespace catchAdmin\report\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\report\model\Declaration as DeclarationModel;
use catcher\CatchAuth;

class Declaration extends CatchController
{
    
    protected $declarationModel;
    
    /**
     *
     * @time 2022/03/07 11:02
     * @param DeclarationModel $declarationModel
     * @return mixed
     */
    public function __construct(DeclarationModel $declarationModel)
    {
        $this->declarationModel = $declarationModel;
    }
    
    /**
     *
     * @time 2022/03/07 11:02
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->declarationModel->getList($user['tenant_id']));
    }
    
    /**
     *
     * @time 2022/03/07 11:02
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->declarationModel->storeBy($params),'新建成功');
    }
    
    /**
     *
     * @time 2022/03/07 11:02
     * @param Request $request
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->declarationModel->updateBy($params['id'], $params),'编辑成功');
    }
    
    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->declarationModel->deleteBy($id),'删除成功');
    }
}